package it.unibo.imola.parser.visitor.entity;

import java.io.File;
import java.io.FileReader;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class ToolBoxFactory {

	private static Logger logger = LoggerFactory.getLogger(ToolBoxFactory.class);
	public static ToolBox createToolBox() throws Exception {
		ToolBox tb = null;
		Properties properties = new Properties();
		File propertyFile = new File("configuration/toolbox.ini");
		if(!propertyFile.exists()){
			if(logger.isDebugEnabled()){
				logger.debug("!propertyFile.exists()");
			}
			throw new Exception("unable to find file "+propertyFile.getAbsolutePath());
		}
		properties.load(propertyFile.toURI().toURL().openStream());
		String tbClass = properties.getProperty("toolbox.class");
		if(logger.isDebugEnabled()){
			logger.debug("loaded class "+tbClass);
		}
		if(tbClass == null || tbClass.isEmpty())
			throw new Exception("tbClass == null || tbClass.isEmpty()");
		try{
			tb = (ToolBox) (Class.forName(tbClass).newInstance());
		}
		catch(Exception e){
			throw new Exception("unable to load ToolBox "+tbClass);
		}
		return tb;
	}
}
